<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 2017/9/5
 * Time: 17:09
 */

namespace app\admin\model;


class AuthGroupAccessModel extends BaseModel
{
    protected $table = 'app_auth_group_access';

    /**
     * 获取用户所有权限
     */
    public function getUserRules($user_id,$pid = 0) {

        $where = array(
            'a.uid' => $user_id,
        );
        //$join = 'LEFT JOIN app_auth_group b ON b.id=a.group_id';
        $join = [['__AUTH_GROUP__ b','b.id=a.group_id','LEFT'],];
        $rules = $this->alias('a')
            ->where($where)
            ->join($join)
            ->field('b.rules')
            ->select();

        if (!$rules) {
            return array();
        }

        $rules_str = '';
        foreach ($rules as $v) {
            $rules_str .= $v['rules'] . ',';
        }

        $rules_str = rtrim($rules_str, ',');

        $rules_arr = array_unique(explode(',', $rules_str));

        $admin_menu_model = new MenuModel();
        $menus = $admin_menu_model->getMenus($rules_arr,$pid?$pid:'');


        //$menus = get_column($menus, 2);
        //dump($rules_arr);exit;

        return $menus;

    }

}